package com.edu.app.service.impl.core;

import com.edu.app.bean.core.SysFile;
import com.edu.app.bean.core.SysRole;
import com.edu.app.bean.core.SysTenant;
import com.edu.app.bean.core.SysUser;
import com.edu.app.bean.core.popup.UsersPop;
import com.edu.app.bean.linkbeans.SysUserRole;
import com.edu.app.mapper.core.SysFileMapper;
import com.edu.app.mapper.core.SysRoleMapper;
import com.edu.app.mapper.core.SysTenantMapper;
import com.edu.app.mapper.core.SysUserMapper;
import com.edu.app.mapper.core.popup.UsersPopMapper;
import com.edu.app.mapper.linkmappers.SysUserRoleMapper;
import com.edu.app.model.UserModel;
import com.edu.app.service.impl.SysBaseServiceImpl;
import com.edu.app.service.sys.core.SysUserService;
import com.edu.app.threadlocal.TenantContext;
import com.edu.app.util.CommonUtils;
import com.edu.app.util.SecurityUtil;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
public class SysUserServiceImpl extends SysBaseServiceImpl<SysUser,SysUserMapper> implements SysUserService {

    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private UsersPopMapper usersPopMapper;

    @Autowired
    private SysTenantMapper sysTenantMapper;

    @Autowired
    private SysFileMapper sysFileMapper;

    @Override
    public Integer add(SysUser user){

        //增加新用户，对密码进行MD5加密
        user.setPassword(SecurityUtil.getEncryptCode(user.getPassword(),user.getUsername()));
        return mapper.insert(user);
    }



    @Override
    public SysUser getUserByName(String userName){
        SysUser user = mapper.getUserByName(userName);
        return user;
    }

    @Override
    public List<UsersPop> getUsersPop(){
        List<UsersPop> usersPop = usersPopMapper.getUsersPop();
        return usersPop;
    }

    @Override
    public List<UsersPop> getUsersPopByName(String name){
        List<UsersPop> usersPop = usersPopMapper.getUsersPopByName(name);
        return usersPop;
    }

    @Override
    public UserModel getUserModelById(Integer id){
        SysUser sysUser = mapper.selectById(id);
        List<SysRole> sysRoleList = sysUserRoleMapper.getRoleListByUserId(id);
        SysTenant sysTenant = sysTenantMapper.selectById(sysUser.getTenant_id());
        SysFile sysFile = sysFileMapper.selectById(sysUser.getHead_img_id());
        return new UserModel(sysUser, sysRoleList,sysTenant,sysFile);
    }

    @Override
    @Transactional
    public void createTenantUser(SysUser user,String company){
        //initialize the tenant obj for the new company
        SysTenant sysTenant = new SysTenant();
        sysTenant.initializeSystemColumns(user.getUsername());
        sysTenant.setCompany(company);
        sysTenant.setType_cd(0);//0 means the trial user
        Date cur = new Date();
        sysTenant.setStart_date(cur);
        sysTenant.setEnd_date(CommonUtils.getFutureMonthDate(cur,1));
        sysTenantMapper.insert(sysTenant);

        //initialize the corresponding user as the seeds user of the new company
        user.initializeSystemColumns(user.getUsername());
        user.setTenant_id(sysTenant.getId());
        //TenantContext.setNeedTenantId(Boolean.FALSE);
        mapper.insert(user);
        //TenantContext.setNeedTenantId(Boolean.TRUE);

    }
    @Override
    @Transactional
    public void assignRole2TenantUser(SysUser sysUser )
    {
        Integer tenant_id = sysUser.getTenant_id();
        SysTenant tenant = sysTenantMapper.selectById(tenant_id);
        TenantContext.setCurrentTenant(tenant);

        //Step1, user role initialize
        SysRole sysRoleAdmin = new SysRole("admin",1,"系统管理员");
        SysRole sysRoleTeacher = new SysRole("teacher",1,"老师");
        SysRole sysRoleConsultant = new SysRole("consultant",1,"销售");
        sysRoleAdmin.initializeSystemColumns(sysUser.getUsername());
        sysRoleTeacher.initializeSystemColumns(sysUser.getUsername());
        sysRoleConsultant.initializeSystemColumns(sysUser.getUsername());
        sysRoleMapper.insert(sysRoleAdmin);
        sysRoleMapper.insert(sysRoleTeacher);
        sysRoleMapper.insert(sysRoleConsultant);
        sysUserRoleMapper.insert(new SysUserRole(sysUser.getId(), sysRoleAdmin.getId()));
        sysUserRoleMapper.insert(new SysUserRole(sysUser.getId(), sysRoleTeacher.getId()));
        sysUserRoleMapper.insert(new SysUserRole(sysUser.getId(), sysRoleConsultant.getId()));

    }

    @Override
    public List<SysUser> getUserListByType(String roleName, Integer status){
        return sysUserMapper.getUserListByType(roleName, status);
    }

    @Override
    public List<SysUser> getUserListForWxNotifications(){
        return sysUserMapper.getUserListForWxNotifications();
    }
}
